package com.xemxoso.thuhien.miennam;

import java.util.ArrayList;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xemxoso.R;
import com.xemxoso.Variables;
import com.xemxoso.jsouplocal.JsoupProvince;
import com.xemxoso.vanthan.mientrung.ItemListProvinceAdapter;
import com.xemxoso.vanthan.mientrung.ItemListProvinceEntity;

public class FragmentMienNam extends Fragment {
	ProgressDialog mProgressDialog;
	int id = 0;
	TextView txttitle;

	ItemListProvinceAdapter madapter;
	ListView list;
	View rootView;

	public FragmentMienNam() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_mien_nam, container,
				false);
		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		txttitle = (TextView) view.findViewById(R.id.text_title_truyen_thong);
		list = (ListView) view.findViewById(R.id.listView_mien_nam);

		madapter = new ItemListProvinceAdapter(getActivity(),
				R.layout.item_list_tinh_custome,
				new ArrayList<ItemListProvinceEntity>());
		list.setAdapter(madapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				Fragment fragment = new ItemListProvince();
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					fragmentManager.beginTransaction()
							.replace(R.id.container, fragment).commit();
				}
			}
		});
		new JsoupMNam().execute(Variables.site);
		super.onViewCreated(view, savedInstanceState);
	}

	public class JsoupMNam extends AsyncTask<String, Void, Void> {
		private ArrayList<ItemListProvinceEntity> mItemProvince;
		private JsoupProvince jProvince;

		@Override
		protected Void doInBackground(String... params) {

			mItemProvince = jProvince.parseJsoupProvince(params[0], 1);
			return null;
		}

		@Override
		protected void onPreExecute() {
			jProvince = new JsoupProvince();
			mItemProvince = new ArrayList<ItemListProvinceEntity>();
			mProgressDialog = new ProgressDialog(getActivity());
			mProgressDialog.setTitle("Please waiting ...");
			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);
			mProgressDialog.show();
		}

		@Override
		protected void onPostExecute(Void result) {

			madapter.addAll(mItemProvince);
			mProgressDialog.dismiss();
			super.onPostExecute(result);
		}

	}

}
